// 显示当前视频统计数据
function displayStats(stats) {
  console.log('准备显示数据:', stats);
  
  const titleContainer = document.getElementById('videoTitle');
  const container = document.getElementById('currentStats');
  
  if (!stats) {
    titleContainer.textContent = '暂无数据';
    container.innerHTML = '<div class="stat-item">请等待数据加载...</div>';
    return;
  }

  titleContainer.textContent = stats.title || '未获取到标题';
  container.innerHTML = `
    <div class="stat-item">
      <span class="stat-label">播放量</span>
      <span class="stat-value">${stats.playCount || '未知'}</span>
    </div>
    <div class="stat-item">
      <span class="stat-label">弹幕数</span>
      <span class="stat-value">${stats.danmakuCount || '未知'}</span>
    </div>
    <div class="stat-item">
      <span class="stat-label">点赞数</span>
      <span class="stat-value">${stats.likeCount || '未知'}</span>
    </div>
    <div class="stat-item">
      <span class="stat-label">投币数</span>
      <span class="stat-value">${stats.coinCount || '未知'}</span>
    </div>
    <div class="stat-item">
      <span class="stat-label">收藏数</span>
      <span class="stat-value">${stats.favoriteCount || '未知'}</span>
    </div>
  `;
}

// 初始显示加载状态
document.addEventListener('DOMContentLoaded', () => {
  displayStats(null);
});

// 当popup打开时，尝试获取数据
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  const currentTab = tabs[0];
  if (currentTab && currentTab.url && currentTab.url.includes('bilibili.com/video')) {
    chrome.tabs.sendMessage(currentTab.id, {type: 'GET_STATS'});
  } else {
    document.getElementById('currentStats').innerHTML = 
      '<div class="stat-item">请在B站视频页面使用此扩展</div>';
  }
});

// 监听来自content script的消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  console.log('收到消息:', message);
  if (message.type === 'VIDEO_STATS') {
    displayStats(message.data);
    // 保存到存储
    chrome.storage.local.set({
      [`video_${Date.now()}`]: message.data
    });
  }
});

document.getElementById('upTab').addEventListener('click', () => {
  window.location.href = 'upinfo.html';
});

document.getElementById('videoTab').addEventListener('click', () => {
  // 已经在视频数据页面，无需操作
}); 